<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use App\Services\AttachmentService;

/**
 * 系统配置表单验证
 * Class ConfigRequest
 * @package App\Http\Requests
 */
class ConfigRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'site.logo_image' => [
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__('config.site_logo_image'), $value, $fail, 1);
                }
            ],
/*            'site.qrcode_image' => [
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__('config.site_qrcode_image'), $value, $fail, 1);
                }
            ],*/
            'site.qrcode_image' => 'nullable',
            'attachment.attachment_upload_ext' => 'nullable|array'
        ];
    }

    /**
     * 提示信息
     * @return array
     */
    public function messages()
    {
        return [
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'update' => ['site.logo_image', 'site.qrcode_image', 'attachment.attachment_upload_ext'],
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'site.logo_image' => __('config.site_logo_image'),
            'site.qrcode_image' => __('config.site_qrcode_image'),
            'attachment.attachment_upload_ext' => __('config.attachment_attachment_upload_ext'),
        ];
    }


    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        // 二维码图集数据处理
        $getQrcodeImage = AttachmentService::getRequestPicture($this->site['qrcode_image']);
        $this->offsetSet('image_list.qrcode_image', $getQrcodeImage['image_list']);
    }


}
